<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head>

  <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"><title>JUnit Tutorial</title>
   <meta content="Jared Richardson" name="author">
	<META name="DESCRIPTION" content="junit tutorial"> 
  <META name="KEYWORDS" content="junit, test automation, java, turorial">
</head>
<body bgcolor="#ffffcc">
	<a href="http://www.jaredrichardson.net/blog">Blog</a>-
	<a href="http://www.jaredrichardson.net/index.html">Main Page</a>
	-
	<a href="http://www.jaredrichardson.net/books.html">Books</a>
	-
	<a href="http://www.jaredrichardson.net/links.html">Links</a>
	-
	<a href="http://www.jaredrichardson.net/resources.html">Resources</a>
	-
	<a href="http://www.jaredrichardson.net/about.html">About</a>
	-
	<a href="http://www.jaredrichardson.net/contact.html">Contact Me</a>
	</p>

	<script type="text/javascript"><!--
	google_ad_client = "pub-9486634274154712";
	google_ad_width = 728;
	google_ad_height = 90;
	google_ad_format = "728x90_as";
	google_ad_type = "text_image";
	google_ad_channel = "";
	//--></script>
	<script type="text/javascript"
	  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
	</script>
<p>
<font face="Times New Roman"><b><big>So what should a JUnit test look like and how do I start using it? </big></b><br>
<br>
by Jared Richardson <a href="http://www.jaredrichardson.net/" target="_blank"><br>
http://www.jaredrichardson.net</a><br>
<br>

<p>
<b>Goals:</b><br>
<br>
This is an extremely brief&nbsp; tutorial on JUnit. The goal is to introduce a new user to basics the JUnit framework.<br>
<br>
<b>Prerequisites:</b><br>
<br>
In order to use this tutorial you will need to have a Java compiler (<a href="http://java.sun.com/" target="_blank">http://java.sun.com/</a>), Ant (<a href="http://ant.apache.org/" target="_blank">http://ant.apache.org/</a>), and JUnit (<a href="http://junit.org/" target="_blank">http://junit.org/</a>) installed. </font><font face="Times New Roman">You will also need JUnit.jar in your Ant's library folder (ANT_HOME/lib).<br>
<br>
I've also included a link to the code and Ant scripts needed to run this example.<br>
<br>
<b>Let's Test Some Code</b><br>
</font>
<p><font face="Times New Roman">First, you need a Java class to test. :) Here's a short example. <br>
</font><br><i><font face="Courier New" size="2">public class Math {</font> <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font face="Courier New" size="2">static public int add(int a, int b) {&nbsp;&nbsp; </font><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font face="Courier New" size="2">return a + b;</font> <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font face="Courier New" size="2">}</font> <br>
<font face="Courier New" size="2">}</font> </i><br>
<br><font face="Times New Roman">My
apologies for the lack of JavaDocs and comments, but it's a pretty
simple bit of code. This class will add two numbers. &nbsp;<br>
<br>
</font><font face="Times New Roman">To test this code, you need a second Java class that will<br>
<br>
&nbsp;&nbsp;&nbsp; 1) import junit.framework.*<br>
&nbsp;&nbsp;&nbsp; 2) extend TestCase. <br>
<br>
That's it! </font></p>

<p><font face="Times New Roman">Here's an example. <br>
</font><i><br>
<font face="Courier New" size="2">import junit.framework.*;</font></i> </p>

<p><i><font face="Courier New" size="2">public class TestMath extends TestCase { </font></i></p>

<p><i><font face="Courier New" size="2">&nbsp; public void testAdd() {</font> <br>
<font face="Courier New" size="2">&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int num1 = 3;</font> <br>
<font face="Courier New" size="2">&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int num2 = 2;</font> <br>
<font face="Courier New" size="2">&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int total = 5;</font> <br>
<font face="Courier New" size="2">&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int sum = 0;</font> <br>
<font face="Courier New" size="2">&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sum = Math.add(num1, num2);</font> <br>
<font face="Courier New" size="2">&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; assertEquals(sum, total);</font> <br>
<font face="Courier New" size="2">&nbsp; }</font> <br>
<font face="Courier New" size="2">}</font>&nbsp;</i></p>

<p><br><font face="Times New Roman">There
are two important points to note in the sample. First, the routine is
named testAddNumbers. This convention tells you that the routine is
supposed to be a test and that it's targetting the "add" functionality.
</font></p>

<p><font face="Times New Roman">The
last step is how to run your JUnit tests. You can do this several ways,
including your command line, Eclipse or the JUnit Test Runner, but I
like plain Ant. To run a Junit test with an Ant script, add this to
your Ant script: </font></p>

<p><i><font face="Courier New" size="2">&lt;junit printsummary="yes" haltonfailure="yes" showoutput="yes" &gt;</font> <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font face="Courier New" size="2">&lt;classpath&gt;</font> <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font face="Courier New" size="2">&lt;pathelement path="${build}"/&gt;</font> <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font face="Courier New" size="2">&lt;/classpath&gt;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font></i></p>

<p><i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font face="Courier New" size="2">&lt;batchtest fork="yes" todir="${reports}/raw/"&gt;</font> <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font face="Courier New" size="2">&lt;formatter type="xml"/&gt;</font> <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font face="Courier New" size="2">&lt;fileset dir="${src}"&gt;</font> <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font face="Courier New" size="2">&lt;include name="**/*Test*.java"/&gt;</font> <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font face="Courier New" size="2">&lt;/fileset&gt;</font> <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font face="Courier New" size="2">&lt;/batchtest&gt;</font> <br>
</i><font face="Courier New" size="2"><i>&lt;/junit&gt;&nbsp;<br>
</i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><br><font face="Times New Roman">Ant
will also do nice things like create nice HTML reports for you! I've
linked to a simple Ant script that will compile all the code in your
"src" folder, run all the tests named "test*" and then create an HTML
report in your "reports\html" folder. Just type "ant test". </font></p>

<p><font face="Times New Roman">I've linked to a zip file (<a href="http://www.jaredrichardson.net/articles/junit-sample.zip">junit-sample.zip</a>)
that contains the build.xml file, the source code and the test class.
(If you try to run it and get errors about JUnit not being found,
remember to add that junit.jar to your Ant lib folder.)</font></p>

<p><font face="Times New Roman"><b>Summary</b> </font></p>

<p><font face="Times New Roman">That's it! This should be all you need to understand the basics of Junit. <br>
<br>
It a lightweight enough framework that this short introduction should
let you do quite a bit. By taking this example you can create a number
of basic, automated tests that can be run from your IDE or in a build
verification system.</font></p>

<p><font face="Times New Roman">For more information, see the </font><a target="_blank" href="http://www.junit.org/news/article/index.htm"><u><font color="#0000ff" face="Times New Roman">JUnit articles page</font></u></a> <font face="Times New Roman">or pick up a copy of <a href="http://www.pragmaticprogrammer.com/starter_kit/utj/index.html" target="_blank">Pragmatic Unit Testing in Java.</a></font></p>

<br>
<script type="text/javascript"><!--
google_ad_client = "pub-9486634274154712";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "text_image";
google_ad_channel = "";
//--></script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

</body></html>